#include "sendmail.h"
#include "ui_sendmail.h"
#include "QFileDialog"
#include "iostream"
#include <string>
#include <QString>
#include "mail.h"
#include "QMessageBox"

using namespace std;
bool DeQuoPri1(const string& _src, string filename) {
    char buf1[100];
    GetModuleFileNameA(NULL, buf1, 100);
    string t = string(buf1);
    string PATH = t.substr(0, t.rfind('\\'))+"\\"+filename;
    remove(&PATH[0]);

    FILE* fp = fopen(PATH.c_str(), "wb");
    if (fp) {
        int len = _src.length();
        unsigned char* buffer = new unsigned char[len];
        char* src = new char[len];
        memcpy(src, _src.c_str(), len);
        int b = 0;
        int pt = 0;
        auto f = [](char c) ->int { return c >= 'A' && c <= 'F' ? c - 'A' + 10 : c - '0'; };
        auto isUpperAlnum = [](char a) {return (a >= 'A' && a <= 'F') || (a >= '0' && a <= '9') ? true : false; };
        for (int i = 0; i < len;) {
            if (src[i] == '=' && src[i + 1] == '\n') {
                memcpy(buffer + pt, src + b, i - b);
                pt = pt + i - b;
                b = i = i + 2;
                continue;
            }
            if (src[i] == '=' && src[i + 1] == '\r' && src[i + 2] == '\n') {
                memcpy(buffer + pt, src + b, i - b);
                pt = pt + i - b;
                b = i = i + 3;
                continue;
            }
            if (src[i] == '=' && isUpperAlnum(src[i + 1]) && isUpperAlnum(src[i + 2])) {
                memcpy(buffer + pt, src + b, i - b);
                pt = pt + i - b;
                unsigned char n = 0;
                n = f(src[i + 1]) * 16 + f(src[i + 2]);
                buffer[pt] = n;
                pt = pt + 1;
                b = i = i + 3;
                continue;
            }
            i++;
        }
        if (b < len) {
            memcpy(buffer + pt, src + b, len - b);
            pt = pt + len - b;
        }
        fwrite(buffer, 1, pt, fp);
        delete[] buffer;
        delete[] src;
        fclose(fp);
        return true;
    }
    else return false;
}
SendMail::SendMail(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::SendMail)
{
    ui->setupUi(this);
}

SendMail::~SendMail()
{
    delete ui;
}

void SendMail::on_pushButton_clicked()
{
    QString filepath = QFileDialog::getOpenFileName(this,"选择单个文件","./");
    ui->textBrowser->setText(filepath);


}

void SendMail::on_pushButton_2_clicked()
{
    QString to=ui->textEdit_2->toPlainText();
    string messagetext=ui->textEdit->toPlainText().toStdString();
    char buf1[100];
    GetModuleFileNameA(NULL, buf1, 100);
    string t = string(buf1);
    string PATH = t.substr(0, t.rfind('\\'))+"\\"+"message";
    remove(&PATH[0]);
    FILE* fp = fopen(PATH.c_str(), "wb");


    fwrite(&messagetext[0], messagetext.size() , 1, fp );
    fclose(fp);

    if(ui->textBrowser->toPlainText().size()==0){
        int argc = 5;
        char* argv[10];
        argv[0]="myemail ";
        argv[1]="send";
        //argv[2]="771582678@qq.com";
        argv[2]=&to.toStdString()[0];
        argv[3]="message";
        argv[4]="utf-8";
        int flag = fx(argc,argv);
        QMessageBox::information(this, tr("success"),  tr("发送成功"),QMessageBox::Save | QMessageBox::Discard,  QMessageBox::Discard);
    } else {
        int argc = 6;
        char* argv[10];
        argv[0]="myemail ";
        argv[1]="send";
        //argv[2]="771582678@qq.com";
        argv[2]=&to.toStdString()[0];
        argv[3]="message";
        argv[4]="utf-8";
        argv[5]=&ui->textBrowser->toPlainText().toStdString()[0];
        int flag = fx(argc,argv);
        QMessageBox::information(this, tr("success"),  tr("发送成功"),QMessageBox::Save | QMessageBox::Discard,  QMessageBox::Discard);
    }
}


